// pages/index/single/single.js
import PagePromoterUtil, { PagePromoter } from '../../../data/common/pagePromoter';
import AppSceneUtil from '../../../data/common/appScene';
import SessionUtil from '../../../data/session/session';
import SystemUtil from '../../../shell/system/system';
const Common = require('../../../utils/common.js');
Page({
    data: {
        tagId: 0,
        tagType_list: {},
        imageSize: [],
    },
    onLoad: function (options) {
        // 开启转发按钮
        PagePromoterUtil.showShareMenu(true).then()
        SessionUtil.getInstance().then(session => {
            // 注册当前页面的场景信息
            AppSceneUtil.setPageScene(this)
            if (options.tagId){
                this.setData({
                    tagId: options.tagId
                });
            }
            this.w_widths();
            this.tagType();
        })
    },
    onShow: function () {
        SessionUtil.getInstance().then(session=>{
            // 注册当前页面的场景信息
            AppSceneUtil.setPageScene(this, false)
            this.w_widths();
            this.tagType();
        })
    },
    /**
     * 加载页面信息
     */
    tagType: function () {
        var page = this;
        Common.my_request({
            url: '/AppMaster/PosSinglePage/getSinglePage',
            data: {
                pageId: page.data.tagId
            },
            success: function (res) {
                if (res.data.result == 0) {
                    var data = res.data.data
                    wx.setNavigationBarTitle({
                        title: data.page_name + ''
                    })
                    var info = data.positions;
                    for(var i = 0; i < info.length; i++){
                        if(info[i].open_data){
                            info[i].open_data = JSON.stringify(info[i].open_data);
                        }
                        
                    }
                    page.setData({
                        tagType_list: info,
                    });
                } else {

                }
            }
        });
    },
    /**
     * 获取屏幕宽度
     */
    w_widths: function () {
        this.setData({
            w_width: SystemUtil.getSystemInfo(false).windowWidth,
        })
    },
    /**
     * 计算图片高度
     */
    imageLoad: function (e) {
        var data = e.currentTarget.id.split("-");
        var index = data[2];
        var page = this;
        var $width = e.detail.width,    //获取图片真实宽度
            $height = e.detail.height,
            ratio = $width / $height;    //图片的真实宽高比例
        var viewWidth = page.data.w_width,           //设置图片显示宽度，左右留有20rpx边距
            viewHeight = viewWidth / ratio;    //计算的高度值
        var imageHeight = page.data.imageSize;

        imageHeight[index]=viewHeight;
        page.setData({
            imageSize: imageHeight
        })
    },
    /**
     * 点击处理
     */
    clicks: function (e) {
        var page = this;
        //var tagValue = e.currentTarget.id;
        var value = e.currentTarget.id.split("-");
        var open_type = value[0],
            data = JSON.parse(value[1])
        switch (open_type) {
            case "item":page.toDetail(data);break;
            case "single_page": page.toSingle(data);break;
            case "book_dinner": page.toDinner(data);break;
            case "none": return false; break;
            default:break;
        }
    },
    toDetail:function(data){
        wx.navigateTo({
            url: '/pages/product/detail/detail?itemId=' + data.item_id
        })
    },
    toSingle: function (data) {
        wx.navigateTo({
            url: '/pages/index/single/single?tagId=' + data.page_id
        })
    },
    toDinner: function (data) {
        wx.navigateTo({
            url: '/pages/index/foodform/index?id=' + data.dr_id
        })
    },
    /**
     * 下拉中断
     */
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },
    onShareAppMessage: function (res) {
        let session = SessionUtil.getInstanceSync();
        // 构造并返回自定义数据
        return (new PagePromoter( this.route, { tagId : this.data.tagId }))
        .setShareTitle(session.getTheSellingServerSync().showName)
        .buildShareInfo()
    },
     
    toIndex() {
        wx.switchTab({
            url: '/pages/index/index',
        })
    }
})